<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
	<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
	<title>结算页</title>

	<link rel="stylesheet" type="text/css" href="../js/shop/css/webbase.css" />
	<link rel="stylesheet" type="text/css" href="../js/shop/css/pages-getOrderInfo.css" />
</head>

<body>
<!--head-->

<div class="cart py-container" id="orderDiv">
	<!--主内容-->
	<div class="checkout py-container">
		<div class="checkout-tit">
			<h4 class="tit-txt">填写并核对订单信息 <a href="cart.html">继续购物</a></h4>
		</div>
		<div class="checkout-steps">
			<!--收件人信息-->
			<div class="step-tit">
				<h5>收件人信息<span><a data-toggle="modal" data-target=".edit" data-keyboard="false" class="newadd" onclick="clearInfo()">新增收货地址</a></span></h5>
			</div>
			<div class="step-cont">
				<div class="addressInfo">
					<ul class="addr-detail">
						<li class="addr-item">
							<div id="receiverDiv"></div>


						</li>

					</ul>
					<!--添加地址-->
					<div  tabindex="-1" role="dialog" data-hasfoot="false" class="sui-modal hide fade edit">
						<div class="modal-dialog">
							<div class="modal-content">
								<div class="modal-header">
									<button type="button" data-dismiss="modal" aria-hidden="true" class="sui-close">×</button>
									<h4 id="myModalLabel" class="modal-title">添加收货地址</h4>
								</div>
								<div class="modal-body">
									<form action="" class="sui-form form-horizontal">
										<div class="control-group">
											<label class="control-label">收货人：</label>
											<div class="controls">
												<input type="text" class="input-medium" id="receiver">
											</div>
										</div>

										<div class="control-group">
											<label class="control-label">详细地址：</label>
											<div class="controls">
												<input type="text" class="input-large" id="address">
											</div>
										</div>
										<div class="control-group">
											<label class="control-label">联系电话：</label>
											<div class="controls">
												<input type="text" class="input-medium" id="phone">
											</div>
										</div>

									</form>


								</div>
								<div class="modal-footer">
									<button type="button" data-ok="modal" class="sui-btn btn-primary btn-large" onclick="submitData()">确定</button>
									<button type="button" data-dismiss="modal" class="sui-btn btn-default btn-large">取消</button>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="hr"></div>

			</div>
			<div class="hr"></div>
			<!--支付和送货-->
			<div class="payshipInfo">
				<div class="step-tit">
					<h5>支付方式</h5>
				</div>
				<div class="step-cont">
					<ul class="payType">
						<li class="selected">微信付款<span title="点击取消选择"></span></li>
						<li>货到付款<span title="点击取消选择"></span></li>
					</ul>
				</div>
				<div class="hr"></div>
				<div class="step-tit">
					<h5>送货清单</h5>
				</div>
				<div class="step-cont">
					<ul class="send-detail">
						<li id="cartBody">

						</li>
					</ul>
				</div>
				<div class="hr"></div>
			</div>
			<div class="linkInfo">
				<div class="step-tit">
					<h5>发票信息</h5>
				</div>
				<div class="step-cont">
					<span>普通发票（电子）</span>
					<span>个人</span>
					<span>明细</span>
				</div>
			</div>
			<div class="cardInfo">
				<div class="step-tit">
					<h5>使用优惠/抵用</h5>
				</div>
			</div>
		</div>
	</div>

	<div id="end">

	</div>

	<div class="submit">
		<a class="sui-btn btn-danger btn-xlarge" href="#" onclick="addOrder()">提交订单</a>
	</div>

</div>

<!-- 底部栏位 -->
<!--页面底部-->
<div class="clearfix footer">
	<div class="py-container">
		<div class="footlink">
			<div class="Mod-service">
				<ul class="Mod-Service-list">
					<li class="grid-service-item intro  intro1">

						<i class="serivce-item fl"></i>
						<div class="service-text">
							<h4>正品保障</h4>
							<p>正品保障，提供发票</p>
						</div>

					</li>
					<li class="grid-service-item  intro intro2">

						<i class="serivce-item fl"></i>
						<div class="service-text">
							<h4>正品保障</h4>
							<p>正品保障，提供发票</p>
						</div>

					</li>
					<li class="grid-service-item intro  intro3">

						<i class="serivce-item fl"></i>
						<div class="service-text">
							<h4>正品保障</h4>
							<p>正品保障，提供发票</p>
						</div>

					</li>
					<li class="grid-service-item  intro intro4">

						<i class="serivce-item fl"></i>
						<div class="service-text">
							<h4>正品保障</h4>
							<p>正品保障，提供发票</p>
						</div>

					</li>
					<li class="grid-service-item intro intro5">

						<i class="serivce-item fl"></i>
						<div class="service-text">
							<h4>正品保障</h4>
							<p>正品保障，提供发票</p>
						</div>

					</li>
				</ul>
			</div>
			<div class="clearfix Mod-list">
				<div class="yui3-g">
					<div class="yui3-u-1-6">
						<h4>购物指南</h4>
						<ul class="unstyled">
							<li>购物流程</li>
							<li>会员介绍</li>
							<li>生活旅行/团购</li>
							<li>常见问题</li>
							<li>购物指南</li>
						</ul>

					</div>
					<div class="yui3-u-1-6">
						<h4>配送方式</h4>
						<ul class="unstyled">
							<li>上门自提</li>
							<li>211限时达</li>
							<li>配送服务查询</li>
							<li>配送费收取标准</li>
							<li>海外配送</li>
						</ul>
					</div>
					<div class="yui3-u-1-6">
						<h4>支付方式</h4>
						<ul class="unstyled">
							<li>货到付款</li>
							<li>在线支付</li>
							<li>分期付款</li>
							<li>邮局汇款</li>
							<li>公司转账</li>
						</ul>
					</div>
					<div class="yui3-u-1-6">
						<h4>售后服务</h4>
						<ul class="unstyled">
							<li>售后政策</li>
							<li>价格保护</li>
							<li>退款说明</li>
							<li>返修/退换货</li>
							<li>取消订单</li>
						</ul>
					</div>
					<div class="yui3-u-1-6">
						<h4>特色服务</h4>
						<ul class="unstyled">
							<li>夺宝岛</li>
							<li>DIY装机</li>
							<li>延保服务</li>
							<li>品优购E卡</li>
							<li>品优购通信</li>
						</ul>
					</div>
					<div class="yui3-u-1-6">
					</div>
				</div>
			</div>
			<div class="Mod-copyright">
				<ul class="helpLink">
					<li>关于我们<span class="space"></span></li>
					<li>联系我们<span class="space"></span></li>
					<li>关于我们<span class="space"></span></li>
					<li>商家入驻<span class="space"></span></li>
					<li>营销中心<span class="space"></span></li>
					<li>友情链接<span class="space"></span></li>
					<li>关于我们<span class="space"></span></li>
					<li>营销中心<span class="space"></span></li>
					<li>友情链接<span class="space"></span></li>
					<li>关于我们</li>
				</ul>
			</div>
		</div>
	</div>
</div>
<!--页面底部END-->

<!--收货地址-->
<div style="display: none" id="infoDiv">
	<div class="con name ##select## "><a href="javascript:;" onclick="updateStatus('##id##')">##receiver##<span title="点击取消选择">&nbsp;</a></div>
	<div class="con address" id="spanDiv" onclick="updateEndDiv('##id##',this)">
		##receiver##
		##address##
		##phone##
		##defutAddr##
		<span class="edittext"><a data-toggle="modal" data-target=".edit" data-keyboard="false" onclick="findById('##id##')" >编辑</a>&nbsp;&nbsp;<a href="javascript:;" onclick="deleteAddress('##id##')">删除</a></span>
	</div>
	<div class="clearfix"></div>
</div>
<!--购物车商品-->
<div style="display:none;" id="cartProductDiv">
	<div class="sendGoods">
		<ul class="yui3-g">
			<li class="yui3-u-1-6">
				<span><img src="##image##" style="height: 80px"/></span>
			</li>
			<li class="yui3-u-7-12">
				<div class="desc">##productName##</div>
				<div class="seven">7天无理由退货</div>
			</li>
			<li class="yui3-u-1-12">
				<div class="price">￥##price##</div>
			</li>
			<li class="yui3-u-1-12">
				<div class="num">X##count##</div>
			</li>
			<li class="yui3-u-1-12">
				<div class="exit">有货</div>
			</li>
		</ul>
	</div>
</div>
<!--底部内容-->
<div style="display: none" id="endDiv">
	<div class="order-summary">
		<div class="static fr">
			<div class="list">
				<span><i class="number">##totalCount##</i>件商品，总商品金额</span>
				<em class="allprice">¥##totalPrice##</em>
			</div>
			<div class="list">
				<span>返现：</span>
				<em class="money">0.00</em>
			</div>
			<div class="list">
				<span>运费：</span>
				<em class="transport">0.00</em>
			</div>
		</div>
	</div>
	<div class="clearfix trade">
		<div class="fc-price">应付金额:　<span class="price">¥##totalPrice##</span></div>
		<div class="fc-receiverInfo" id="addressDiv">寄送至:##address## 收货人：##receiver## ##phone##</div>
	</div>
</div>


<script type="text/javascript" src="../js/shop/js/plugins/jquery/jquery.min.js"></script>
<script type="text/javascript" src="../js/shop/js/plugins/jquery.easing/jquery.easing.min.js"></script>
<script type="text/javascript" src="../js/shop/js/plugins/sui/sui.min.js"></script>
<script type="text/javascript" src="../js/shop/js/pages/getOrderInfo.js"></script>
<script src="../js/common.js"></script>
<script>
	var v_token;
	$(function (){
		v_token = sessionStorage.getItem(TOKEN);
		initAddress();
		initCart();
		endDiv();
		idempotency();
	})

	//判断幂等性
	var x_token
	function idempotency(){
		if (v_token) {
			$.ajax({
				type: "post",
				url: API_URL + "tokens",
				beforeSend: function (xhr) {
					xhr.setRequestHeader(X_AUTH, v_token);
				},
				success:function (res){
					console.log(res)
					if (res.code == 200){
						x_token = res.data;
					}
				}
			})
		}
	}

	//增加订单  提交订单
	function addOrder(){
		if (v_token) {
			var v_param = {};
			v_param.addressId = v_currAddress.id;
			v_param.payType = 1;
			$.ajax({
				type: "post",
				url: API_URL + "orders",
				data:v_param,
				beforeSend: function (xhr) {
					xhr.setRequestHeader(X_AUTH, v_token);
					xhr.setRequestHeader(X_IDEMPOTENCY, x_token);
				},
				success:function (res){
					if (res.code == 200){
						location.href = "pay.html";
					}
				}
			})
		}else {
			alert("请先登录")
		}
	}

	//清空增加时的旧数据
	function clearInfo(){
		$("#receiver").val("");
		$("#address").val("");
		$("#phone").val("");
		$("#myModalLabel").html("新增收货地址")
		address=null;
	}

	//修改底部地址
	function updateEndDiv(id,obj){
		//修改地址
		for (let v_address of v_addressList) {
			if (v_address.id == id){
				$("#addressDiv").html("寄送至:"+v_address.address+" 收货人："+v_address.receiver+" "+v_address.phone)
				v_currAddress = v_address;
				break;
			}
		}

		$(".con.address").css({"font-weight":"","color": ""})

		$(obj).css({"font-weight":"bold","color": "red"})


		// $("#addressDiv").html("寄送至:"+$($("span",obj)[0]).html()+" 收货人："+$($("span",obj)[1]).html() +" "+$($("span",obj)[2]).html())
	}


	//底部内容
	function endDiv() {
		var v_endDiv = $("#endDiv").html()
				.replace(/##totalCount##/g, cartItem.totalCount)
				.replace(/##totalPrice##/g, cartItem.totalSumPrice)
				.replace(/##receiver##/g, v_currAddress.receiver)
				.replace(/##address##/g, v_currAddress.address)
				.replace(/##phone##/g, v_currAddress.phone);
		$("#end").html(v_endDiv);
	}

	//购物车
	var cartItem;
	function initCart(){
		if (!v_token){
			$("#cartListDiv").html('<h1 align="center">请<a style="color: #003eff" href="login.html">登录</a>后在操作</h1>');
		}
		$.ajax({
			type:"get",
			url:API_URL+"carts",
			async:false,
			beforeSend:function (xhr){
				xhr.setRequestHeader(X_AUTH,v_token);
			},
			success:function (res){
				if (res.code == 200){
					cartItem = res.data;
					var v_cartList = cartItem.cartItemVoList;
					var v_html = '';
					for (let v_cart of v_cartList) {
						v_html += $("#cartProductDiv").html()
								.replace(/##image##/g,v_cart.image)
								.replace(/##productName##/g,v_cart.productName)
								.replace(/##price##/g,v_cart.price)
								.replace(/##count##/g,v_cart.count)
					}

					$("#cartBody").html(v_html);
				}
			}
		})
	}

	//修改状态
	function updateStatus(id){
		if (v_token){
			$.ajax({
				type:"post",
				url:API_URL+"address/updateStatus",
				data:{"id":id},
				beforeSend:function (xhr){
					xhr.setRequestHeader(X_AUTH,v_token);
				},
				success:function (res){
					if (res.code == 200){
						initAddress();
						endDiv();
					}
				}
			})
		}else {
			alert("请登录后操作！")
		}
	}

	//查询所有收货地址
	var v_currAddress;
	var v_addressList;
	function initAddress(){
		if (v_token){
			$.ajax({
				type:"get",
				url:API_URL+"address",
				async:false,
				beforeSend:function (xhr){
					xhr.setRequestHeader(X_AUTH,v_token);
				},
				success:function (res){
					if (res.code == 200){
						v_addressList = res.data;
						var v_html = '';
						for (let v_address of v_addressList) {
							v_html += $("#infoDiv").html()
									.replace(/##select##/g,v_address.status == 1?"selected":"")
									.replace(/##defutAddr##/g,v_address.status == 1?"<span class=\"base\">默认地址</span>":"")
									.replace(/##id##/g,v_address.id)
									.replace(/##receiver##/g,v_address.receiver)
									.replace(/##address##/g,v_address.address)
									.replace(/##phone##/g,v_address.phone);
							if (v_address.status == 1){
								v_currAddress = v_address;
							}
						}
						$("#receiverDiv").html(v_html).append();
						initHover();
					}else {
						alert(res.msg)
					}
				}
			})
		}else {
			location.href = "login.html";
		}
	}

	function initHover(){
		$(".address").hover(function(){
			$(this).addClass("address-hover");
		},function(){
			$(this).removeClass("address-hover");
		});
	}

	//回显
	var address;
	function findById(id){
		$("#myModalLabel").html("修改收货地址")
		if (v_token){
			$.ajax({
				type:"get",
				url:API_URL+"address/"+id,
				beforeSend:function (xhr){
					xhr.setRequestHeader(X_AUTH,v_token);
				},
				success:function (res){
					if (res.code == 200){
						address = res.data;
						$("#receiver").val(address.receiver);
						$("#address").val(address.address);
						$("#phone").val(address.phone);
					}
				}
			})
		}else {
			alert("请登录后操作！")
		}
	}

	//新增 修改
	function submitData(){
		if (v_token){
			if (address){
				//修改
				var v_param = {};
				v_param.id=address.id;
				v_param.receiver=$("#receiver").val();
				v_param.address=$("#address").val();
				v_param.phone=$("#phone").val();
				$.ajax({
					type:"put",
					url:API_URL+"address",
					data:JSON.stringify(v_param),
					contentType:"application/json",
					beforeSend:function (xhr){
						xhr.setRequestHeader(X_AUTH,v_token);
					},
					success:function (res){
						if (res.code == 200){
							initAddress();

						}
					}
				})
			}else {
				//新增
				var v_param = {};
				v_param.receiver=$("#receiver").val();
				v_param.address=$("#address").val();
				v_param.phone=$("#phone").val();
				$.ajax({
					type:"post",
					url:API_URL+"address",
					data:v_param,
					beforeSend:function (xhr){
						xhr.setRequestHeader(X_AUTH,v_token);
					},
					success:function (res){
						if (res.code == 200){
							initAddress();
						}
					}
				})
			}
		}else {
			alert("请登录后操作！")
		}
	}

	//删除
	function deleteAddress(id){
		if (v_token){
			$.ajax({
				type:"delete",
				url:API_URL+"address",
				data:{"id":id},
				beforeSend:function (xhr){
					xhr.setRequestHeader(X_AUTH,v_token);
				},
				success:function (res){
					if (res.code == 200){
						initAddress();
					}
				}
			})
		}else {
			alert("请登录后操作！")
		}
	}

</script>
</body>
</html>